<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\service\notice;

use app\enum\NoticeEnum;
use app\model\mysql\NoticeModel;
use app\service\BaseService;
use think\Model;

final class NoticeService extends BaseService
{
    private static array $handle = [];

    /**
     * 获取实例.
     * @param int $type 类型
     * @param array $conf 配置
     */
    public static function getInstance(int $type = 0, array $conf = [])
    {
        $key = md5($type . json_encode($conf));
        if (! isset(self::$handle[$key])) {
            $classname = NoticeEnum::NOTICE_CLASS[$type] ?? '';
            if (! $classname) {
                return self::setError('未知通知类型');
            }
            self::$handle[$key] = new $classname($conf);
        }
        return self::$handle[$key];
    }

    /**
     * 获取提醒.
     * @param int $id id
     * @return array|mixed|Model|NoticeModel
     */
    public static function getNotice(int $id = 0)
    {
        return (new NoticeModel())->where('id', $id)->findOrEmpty();
    }
}
